package com.usabb.api;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ifast.common.base.BaseController;
import com.ifast.common.utils.Result;
import com.usabb.domain.HospitalDO;
import com.usabb.service.HospitalApiService;
import com.usabb.service.HospitalService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.Pattern;

/**
 * 医院
 */
@Slf4j
@RequestMapping("usabb/api/hospital")
@Controller
public class HospitalApiController extends BaseController {


    @Autowired
    private HospitalApiService hospitalApiService;
    @Autowired
    private HospitalService hospitalService;

    @CrossOrigin
    @ResponseBody
    @GetMapping("{id}")
    public Result<HospitalDO> getHomeStay(@Valid @Pattern(regexp = "[0-9]+") @PathVariable(name = "id") Long id) {
        HospitalDO fullDO = hospitalApiService.getFullDOById(id);
        return Result.ok(fullDO);
    }

    @CrossOrigin
    @GetMapping("list")
    @ResponseBody
    public Result<Page<HospitalDO>> getList() {
        HospitalDO doParam = new HospitalDO();
        Wrapper<HospitalDO> wrapper = new EntityWrapper<>(doParam).orderBy("id", false);

        Page<HospitalDO> page = hospitalService.selectPage(getPage(HospitalDO.class), wrapper);
        page.getRecords().forEach(it -> hospitalApiService.appendDetail(it));

        return Result.ok(page);
    }

}
